package cn.wei.reallyenjoy.diary.service.impl;

import cn.wei.reallyenjoy.common.entity.Diary;
import cn.wei.reallyenjoy.diary.mapper.DiaryMapper;
import cn.wei.reallyenjoy.diary.pojo.vo.DiaryIndexVO;
import cn.wei.reallyenjoy.diary.pojo.vo.WriteDiaryVO;
import cn.wei.reallyenjoy.diary.service.DiaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 日记表 服务实现类
 * </p>
 *
 * @author Wei
 * @since 2021-11-28
 */
@Service
public class DiaryServiceImpl extends ServiceImpl<DiaryMapper, Diary> implements DiaryService {

    @Override
    public List<DiaryIndexVO> getDiaryList() {
        return baseMapper.getDiaryList();
    }

    @Override
    public void updateDiaryPreferById(Long diaryId, Boolean status) {

        Diary diary = baseMapper.selectById(diaryId);
        Diary diaryUpdate = new Diary();
        diaryUpdate.setId(diaryId);
        if (status) {
            //取消收藏，日记收藏数量-1
            diaryUpdate.setPrefer(diary.getPrefer() - 1);
        }else{
            //收藏，日记收藏数量+1
            diaryUpdate.setPrefer(diary.getPrefer() + 1);
        }
        baseMapper.updateById(diaryUpdate);
    }

    @Override
    public List<DiaryIndexVO> getDiaryByUserId(Long userId) {
        return baseMapper.getDiaryByUserId(userId);
    }

    @Override
    public List<DiaryIndexVO> CollectionByUid(Long userId) {
        return baseMapper.CollectionByUid(userId);
    }

    @Override
    public int deleteDiaryByUidAndDiaryId(Long userId, Long diaryId) {
        QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
        diaryQueryWrapper.eq("user_id",userId).eq("id",diaryId);
        return  baseMapper.delete(diaryQueryWrapper);
    }

    @Override
    public void addDiary(WriteDiaryVO writeDiaryVO) {
        Diary diary = new Diary();
        diary.setUserId(writeDiaryVO.getUid());
        diary.setTitle(writeDiaryVO.getTitle());
        diary.setContent(writeDiaryVO.getContent());
        diary.setContentImg(writeDiaryVO.getContentImg());
        baseMapper.insert(diary);
    }
}
